package com.finddeer.bookkeeping.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.finddeer.bookkeeping.Bill;
import com.finddeer.bookkeeping.BillLab;
import com.finddeer.bookkeeping.BillPagerActivity;
import com.finddeer.bookkeeping.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author    : leaves
 * gitee     : https://gitee.com/LEAVES2018
 * time      : 2018/12/23 8:27 PM
 * function  : 暂时废弃。
 */
// TODO 解决viewPager不能及时刷新的问题
public class BookkeepingContentFragment extends Fragment {

    private RecyclerView mBillRecyclerView;
    private BillAdapter mBillAdapter;

    @BindView(R.id.txt_content)
    TextView mContentTextView;

    private static final String ARG_LIST_NAME = "list_name";

    private Button mRefreshButton;

    private String name;

    public static BookkeepingContentFragment newInstance(String listName) {
        BookkeepingContentFragment fragment = new BookkeepingContentFragment(); //  创建一个新的内容Fragment
        Bundle args = new Bundle();
        args.putString(ARG_LIST_NAME, listName);  //  存储对应位置的字符串，0-昨日，1-今天，2-预算
        fragment.setArguments(args);  //  设置fragment的arguments
        return fragment;    //  返回带有arguments的fragment
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle args = getArguments();
        name = args.getString("name");
        if (name == null) {
            name = "参数非法";
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bookkeeping_content, container, false);
        ButterKnife.bind(this, view);

        mContentTextView.setText(name);

        mBillRecyclerView = view.findViewById(R.id.bill_recycler_view);
        mBillRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        mRefreshButton = view.findViewById(R.id.refresh_button);
        mRefreshButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateUI();
            }
        });

        updateUI();
        return view;
    }

    private void updateUI() {
        BillLab billLab = BillLab.get(getActivity());
        List<Bill> bills = billLab.getBills();

        if (mBillAdapter == null) {
            mBillAdapter = new BillAdapter(bills);
            mBillRecyclerView.setAdapter(mBillAdapter);
        } else {
            mBillAdapter.setBills(bills);
            mBillAdapter.notifyDataSetChanged();
        }
    }

    private class BillHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener {
        private TextView mTitleTextView;
        private TextView mDetailsTextView;
        private TextView mAmountTextView;
        private TextView mPriceUnitTextView;

        private Bill mBill;

        public BillHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.list_item_bill, parent, false));
            mTitleTextView = itemView.findViewById(R.id.bill_title);
            mDetailsTextView = itemView.findViewById(R.id.bill_details);
            mAmountTextView = itemView.findViewById(R.id.bill_amount);
            mPriceUnitTextView = itemView.findViewById(R.id.bill_price_unit);
            itemView.setOnClickListener(this);
        }

        public void bind(Bill bill) {
            mBill = bill;

            mTitleTextView.setText(mBill.getTitle());
            mDetailsTextView.setText(mBill.getLocale() + " " + mBill.getTag()); //  显示消费地点以及标签信息
            mAmountTextView.setText(mBill.getAmount()+"");
            mPriceUnitTextView.setText(R.string.price_unit);
        }


        @Override
        public void onClick(View v) {
            Intent intent = BillPagerActivity.newIntent(getActivity(), mBill.getId());
            startActivity(intent);
        }
    }

    private class BillAdapter extends RecyclerView.Adapter<BillHolder> {
        private List<Bill> mBills;

        public BillAdapter(List<Bill> bills) {
            mBills = bills;
        }

        @NonNull
        @Override
        public BillHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());

            return new BillHolder(layoutInflater, parent);
        }

        @Override
        public void onBindViewHolder(@NonNull BillHolder holder, int position) {
            Bill bill = mBills.get(position);
            holder.bind(bill);
        }

        @Override
        public int getItemCount() {
            return mBills.size();
        }

        public void setBills(List<Bill> bills) {
            mBills = bills;
        }
    }

    @Override

    public void onResume() {
        super.onResume();
        updateUI();
    }

}
